package com.bbx.taxi.client.widget.Drag;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baidu.mapapi.map.MapView;
import com.bbx.api.sdk.model.passanger.Return.Elements;
import com.bbx.api.sdk.model.passanger.Return.MeteredFee;
import com.bbx.api.sdk.model.passanger.Return.PriceDetail;
import com.bbx.api.sdk.model.passanger.Return.ShareDetail;
import com.bbx.api.sdk.model.passanger.Return.UsableSale;
import com.bbx.api.sdk.model.passanger.Return.CarType.CarData;
import com.bbx.api.sdk.model.passanger.Return.CarType.CarType;
import com.bbx.taxi.client.R;
import com.bbx.taxi.client.Activity.Main.IMainContract;
import com.bbx.taxi.client.Activity.Main.MainPresenter;
import com.bbx.taxi.client.Activity.Main.MainPresenter.OnCouponFinish;
import com.bbx.taxi.client.Adapter.PayAdapter;
import com.bbx.taxi.client.Adapter.PayDetailsAdapter;
import com.bbx.taxi.client.Bean.Attribute.MainAttribute;
import com.bbx.taxi.client.Util.CouponUtils;
import com.bbx.taxi.client.Util.DateFormat;
import com.bbx.taxi.client.Util.FormatUtil;
import com.bbx.taxi.client.Util.VersionUtils;
import com.bbx.taxi.client.Util.FastBlur.BlurBuilder;
import com.bbx.taxi.client.Util.FastBlur.BlurBuilder.OnBlurFinishListener;
import com.bbx.taxi.client.Util.FastBlur.BlurBuilder.OnSnapShotFinishListener;
import com.bbx.taxi.client.widget.pop.SharePopView.OnShareDetaiFinish;
import com.yanxuwen.loadview.LoadingDialog;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.media.Image;
import android.os.Handler;
import android.support.v4.view.MotionEventCompat;
import android.support.v4.view.ViewCompat;
import android.support.v4.widget.ViewDragHelper;
import android.text.Html;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

/** 
 * @Path com.bbx.taxi.client.widget.Drag.FareDetailDragLayout.java
 * @Author 严旭文
 * @Date 2016-9-28 下午2:24:07
 * @Company 蓝海（福建）信息技术有限公司
 * @Description
 */
public class FareDetailDragLayout extends BaseDragLayout implements OnSnapShotFinishListener,OnBlurFinishListener,OnCouponFinish{

	private TextView tv_pay;
	private TextView tv_combo;
	private TextView tv_night;
	private TextView tv_time;
	private RelativeLayout layout_night;
	private TextView tv_default;
	private ImageView iv_biaozhi;
	private ImageView iv_exit;
	private RelativeLayout layout_fare;
	/**支付的费用明细列表*/
	private ListView lv_paydetail;

	private PayDetailsAdapter mPayDetailsAdapter;
	/**支付费用明细列表*/
	private List<Map<String, String>> list_paydetail=new ArrayList<Map<String,String>>();
	private IMainContract.Presenter mPresenter;
	LoadingDialog mLoadingDialog;
	
	public FareDetailDragLayout(Context context) {
		super(context);
	}

	public FareDetailDragLayout(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
		this.context=context;
	}

	public FareDetailDragLayout(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

	@Override
	public void initView() {
		setContentView(findViewById(R.id.desc_fare));
		layout_fare= (RelativeLayout) getContentView().findViewById(R.id.layout_fare);
		tv_pay = (TextView) getContentView().findViewById(R.id.tv_pay);
		tv_default=(TextView)getContentView().findViewById(R.id.tv_default);
		tv_combo=(TextView)getContentView().findViewById(R.id.tv_combo);
		tv_night=(TextView)getContentView().findViewById(R.id.tv_night);
		tv_time=(TextView)getContentView().findViewById(R.id.tv_time);
		layout_night=(RelativeLayout)getContentView().findViewById(R.id.layout_night);
		iv_biaozhi=(ImageView)getContentView().findViewById(R.id.iv_biaozhi);
		iv_biaozhi.setVisibility(View.GONE);
		iv_exit=(ImageView)findViewById(R.id.iv_exit);
		iv_exit.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				close();
			}
		});
		lv_paydetail=(ListView)findViewById(R.id.lv_paydetail);
		
		mPayDetailsAdapter=new PayDetailsAdapter(context, list_paydetail);
		lv_paydetail.setAdapter(mPayDetailsAdapter);
		BlurBuilder.setOnSnapShotFinishListener(this);
		BlurBuilder.setBlurFinishListener(this);
		MainPresenter.setOnCouponFinish(this);
	}
	public void setLoadView(LoadingDialog mLoadingDialog){
		this.mLoadingDialog=mLoadingDialog;
		if(mLoadingDialog!=null&&!mLoadingDialog.isShowing()){
			mLoadingDialog.show();
		}
	}
	/**优惠劵金额，由于有时候优惠劵金额会为0元，所以优惠劵加载完毕后要马上在这个界面显示*/
	double coupon_sum;double pay;double fare;double thanks_fee;
	/**
	 * 传递以分为单位，自动转化为元
	 * @param pay  预约价格
	 * @param fare 车费价格
	 * @param coupon_sum  优惠劵 【红色标注并且用+】
	 * @param thanks_fee  感谢费 【红色标注并且用-】
	 */
	public void setDetail(double pay,double fare,double coupon_sum,double thanks_fee,int sale_type,double base_discount){
		this.pay=pay;
		this.fare=fare;
		this.coupon_sum=coupon_sum;
		this.thanks_fee=thanks_fee;
		tv_pay.setText(FormatUtil.onFormatPrice(pay));
		list_paydetail.clear();
		
		//打折
		String str_discount ="";
		if(base_discount>1){
			str_discount=""+base_discount+"倍";
		}else if(base_discount<1){
			str_discount=""+FormatUtil.onFormatDecimal2(base_discount*10)+"折";
		}
		addPayDetail("车费预计",FormatUtil.onFormatPrice(fare)+"",PayDetailsAdapter.TYPE_PRICE,"",str_discount);
		int schedule_fee_id = 0;
		switch(VersionUtils.getVersionType(context)){
		case bbx:
			schedule_fee_id=R.string.schedule_fee;
			break;
		case tianshang:
			schedule_fee_id=R.string.schedule_fee2;
			break;
		}
		addPayDetail(context.getString(schedule_fee_id),FormatUtil.onFormatPrice(thanks_fee)+"",PayDetailsAdapter.TYPE_THANK,"","");
		switch(sale_type){
    	case UsableSale.sale_type_no:
    		addPayDetail("优惠劵抵扣",FormatUtil.onFormatPrice(coupon_sum)+"",PayDetailsAdapter.TYPE_REDUCE,"","");
    		break;
    	case UsableSale.sale_type_submit:
    		addPayDetail("下单立减优惠",FormatUtil.onFormatPrice(coupon_sum)+"",PayDetailsAdapter.TYPE_REDUCE,"","");
    		break;
    	case UsableSale.sale_type_online:
    		addPayDetail("线上支付立减",FormatUtil.onFormatPrice(coupon_sum)+"",PayDetailsAdapter.TYPE_REDUCE,"","");
    		break;
    	}
		mPayDetailsAdapter.notifyDataSetChanged();
		
	}
	public void setCombo(IMainContract.Presenter mPresenter,CarData mCarData,MeteredFee mMeteredFee,int business_type,int calc_type){
		this.mPresenter=mPresenter;
		MainAttribute main=MainAttribute.getInstance();
		if(mCarData==null||mMeteredFee==null)return;
		if(mCarData.getCartTypeNight(DateFormat.onDate_Long(main.getTime(mPresenter.isCity())))){
			tv_night.setText("夜间计费");
			long start_time=mCarData.getNightStartTime();
			long end_time=mCarData.getNightEndTime();
			String srt_start_time=(start_time/3600)+":"+((start_time%3600)/60);
			String srt_end_time=(end_time/3600)+":"+((end_time%3600)/60);
			tv_time.setText("("+srt_start_time+"~"+srt_end_time+")");
			layout_night.setVisibility(View.VISIBLE);
		}else{
			layout_night.setVisibility(View.GONE);
		}
		double combo_price = mCarData.getComboPrice(DateFormat.onDate_Long(main.getTime(mPresenter.isCity())));
		double default_miles = mCarData.getDefaultMiles(DateFormat.onDate_Long(main.getTime(mPresenter.isCity())));
		double default_minutes = mCarData.getDefaultMinutes(DateFormat.onDate_Long(main.getTime(mPresenter.isCity())));
		double time_price = mCarData.getTimePrice(DateFormat.onDate_Long(main.getTime(mPresenter.isCity())));
		double mile_price = mCarData.getMilePrice(DateFormat.onDate_Long(main.getTime(mPresenter.isCity())));
		double expect_miles = mMeteredFee.miles;
		double expect_minutes = mMeteredFee.minutes;
		
		
		if((mPresenter.isBC()&&business_type!=CarType.BUSUNESS_TYPE_dan)||(mPresenter.isCity()&&calc_type==CarType.CALC_TYPE_jifei)){
			//根据是否为0显示3种情况
			double double_default_miles=0;
			try{double_default_miles=Double.parseDouble(FormatUtil.onFormatDecimal(default_miles/1000));}catch(Exception e){}
			double double_default_minutes=0;
			try{double_default_minutes=Double.parseDouble(FormatUtil.onFormatDecimal(default_minutes));}catch(Exception e){}
			String combo_exceed="";
			if(double_default_miles!=0&&double_default_minutes!=0){
				combo_exceed=String.format(context.getString(R.string.combo_exceed_miles_and_time), double_default_miles,double_default_minutes);
			}else if(double_default_miles!=0&&double_default_minutes==0){
				combo_exceed=String.format(context.getString(R.string.combo_exceed_miles), double_default_miles);
			}else if(double_default_miles==0&&double_default_minutes!=0){
				combo_exceed=String.format(context.getString(R.string.combo_exceed_time), double_default_minutes);
			}
			
			tv_combo.setText(String.format(context.getString(R.string.combo_price), 
					FormatUtil.onFormatPrice(combo_price),
					combo_exceed,
					FormatUtil.onFormatPrice(time_price),
					FormatUtil.onFormatPrice(mile_price)));
			tv_default.setText(String.format(context.getString(R.string.default_miles_minutes),
					FormatUtil.onFormatDecimal(expect_miles/1000),FormatUtil.onFormatDecimal(expect_minutes)));
			tv_combo.setVisibility(View.VISIBLE);
			tv_default.setVisibility(View.VISIBLE);
		}else{
			tv_combo.setVisibility(View.GONE);
			tv_default.setVisibility(View.GONE);
		}

		
	}
	private void addPayDetail(String name, String money, String type,
			String mark,String base_discount) {
		String str_mark ="";
		str_mark= CouponUtils.getMark(context, mark, false);
		Map<String, String> map = new HashMap<String, String>();
		map.put(PayDetailsAdapter.PAYDETAIL_NAME, name);
		map.put(PayDetailsAdapter.PAYDETAIL_MONEY, money);
		map.put(PayDetailsAdapter.PAYDETAIL_TYPE, type);
		//如果是线上专享却是点击线下支付，则不显示
		if(str_mark!=null&&!str_mark.equals("")){
		map.put(PayDetailsAdapter.PAYDETAIL_MARK, str_mark);
		}
		map.put(PayDetailsAdapter.PAYDETAIL_DISCOUNT, base_discount);
		list_paydetail.add(map);
	}


	@Override
	public void OnSnapShotFinish(boolean isSuccess) {
		if(isSuccess){
			BlurBuilder.blur((Activity)context, layout_fare);
		}
	}

	@Override
	public void OnBlurFinish(final Drawable mDrawable) {
		if(mLoadingDialog!=null){
			mLoadingDialog.dismiss();
			mLoadingDialog=null;
		}
		new Handler().postDelayed(new Runnable() {
			@Override
			public void run() {
				layout_fare.setBackgroundDrawable(mDrawable);
				open();
			}
		}, 500);
		
	}

	@Override
	public void onCouponFinish(double coupon_sum) {
		setDetail(pay, fare, coupon_sum* 100, thanks_fee,mPresenter.getSaleType(),mPresenter.getBaseDiscount());
	}

	@Override
	protected void onViewStatus(boolean isOpen) {
		// TODO Auto-generated method stub
		
	}
}
